import request from '@/utils/request'

// 商品接口类型
interface Product {
  id: number;
  name: string;
  description: string;
  avatar: string;
  productType: number;
  price: number;
  salesVolume: number;
  productionDate: string;
  expirationDate: string;
  supplierId: number;
  createdBy: string;
  createdTime: string;
  updateBy: string;
  updateTime: string;
  quantity: number | null;
  shoppingCartId: number | null;
  isDeleted: boolean | null;
}

// 定义请求参数接口
interface ProductQueryParams {
  pageNo: number;
  pageSize: number;
  productType?: number;
  isHot?: string;
  isPrice?: string;
}

// 获取热门排行榜
export function getHotRank() {
  return request({
    url: '/agriculturalProduct/queryHotRank',
    method: 'get'
  })
}

// 获取商品列表
export function getProductList(params: ProductQueryParams) {
  return request({
    url: '/agriculturalProduct/queryByProductType',
    method: 'post',
    data: params
  })
}

interface QueryParams {
  pageNo: number
  pageSize: number
  productType: number
  isHot: boolean
}

// 按类型查询农产品
export const queryByProductType = (params: any) => {
  return request({
    url: '/agriculturalProduct/queryByProductType',
    method: 'post',
    data: params
  })
}

// 获取单个农产品详情
export const getProductById = (id: number | string) => {
  return request({
    url: `/agriculturalProduct/${id}`,
    method: 'get'
  })
}

// 添加到购物车
export const addToCart = (data: {
  quantity: number
  productId: number | string
}) => {
  return request({
    url: '/shoppingCart/add',
    method: 'post',
    data
  })
}

// 获取农产品详情
export const getProductDetail = (id: string | number) => {
  return request({
    url: `/agriculturalProduct/${id}`,
    method: 'get'
  })
}

// 搜索农产品
export const searchProducts = (data: { searchKey: string }) => {
  return request({
    url: '/agriculturalProduct/search',
    method: 'post',
    data
  })
}

export const queryHotRank = () => {
  return request({
    url: "/product/hotRank",
    method: "get",
  });
}; 